﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace SynoManager.DownloadStation.Subscriptions
{
    public class SubscriptionManager : IEnumerable<Subscription>
    {
        private readonly SubscriptionsGuiPlugin plugin;

        public SubscriptionManager(SubscriptionsGuiPlugin plugin)
        {
            this.plugin = plugin;
            Load();
        }

        #region Subscription List
        private List<Subscription> subscriptions = new List<Subscription>();
        
        public void Add(Subscription subscription)
        {
            Debug.WriteLine("Add()", "SubscriptionManager");
            subscriptions.Add(subscription);
            Save();
        }

        public bool Remove(Subscription subscription)
        {
            Debug.WriteLine("Remove()", "SubscriptionManager");
            bool result = subscriptions.Remove(subscription);
            Save();
            return result;
        }

        public int Count
        {
            get { return subscriptions.Count; }
        }
        #endregion

        #region Store Functions (Save/Load)
        private LocalSubscriptionStore store = new LocalSubscriptionStore();

        public void Save()
        {
            Debug.WriteLine("Save()", "SubscriptionManager");
            store.Save(subscriptions.ToArray());
        }

        public void Load()
        {
            Debug.WriteLine("Load()", "SubscriptionManager");
            subscriptions = new List<Subscription>(store.Load());
        }
        #endregion

        #region IEnumerable<Subscription> Members
        public IEnumerator<Subscription> GetEnumerator()
        {
            return subscriptions.GetEnumerator();
        }

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return subscriptions.GetEnumerator();
        }
        #endregion

        #region Check Functions
        public void CheckAll()
        {
            Debug.WriteLine("CheckAll()", "SubscriptionManager");
            foreach (Subscription subscription in subscriptions)
            {
                Check(subscription);
            }
        }

        public void AutoCheck()
        {
            Debug.WriteLine("AutoCheck()", "SubscriptionManager");
            foreach (Subscription subscription in subscriptions)
            {
                if (subscription.AutoUpdate)
                {
                    if (subscription.LastCheck.Add(subscription.UpdateInterval) < DateTime.Now)
                    {
                        Check(subscription);
                    }
                }
            }
        }

        public void Check(Subscription subscription)
        {
            Debug.WriteLine("Check()", "SubscriptionManager");
            subscription.Update();

            foreach(SubscriptionItem item in subscription.Items)
            {
                if (item.DatePublished > subscription.LastCheck)
                    Download(item);
            }

            subscription.LastCheck = DateTime.Now;
        }

        public bool Download(SubscriptionItem item)
        {
            Debug.WriteLine("Download()", "SubscriptionManager");
            string url = item.GetDownloadLink();
            if (url == null)
            {
                item.State = SubscriptionItemState.Error;
                return false;
            }
            else
            {
                item.State = SubscriptionItemState.Downloaded;
                plugin.DownloadStation.AddUrl(url);
                return true;
            }
        }
        #endregion

    }
}
